package com.assignwork.service.impl;

import com.assignwork.dto.StartedCabinDTO;
import com.assignwork.entity.StartedCabin;
import com.assignwork.mapper.StartedCabinMapper;
import com.assignwork.service.IStartedCabinService;
import com.assignwork.vo.StartedCabinVO;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 作业舱实现类
 */
@Service
public class StartedCabinServiceImpl extends BaseServiceImpl<StartedCabinMapper, StartedCabin> implements IStartedCabinService {

    @Override
    public boolean saveOrUpdateDtoList(List<StartedCabinDTO> dtos) {
        if (Func.isEmpty(dtos)) {
            return false;
        }
        List<StartedCabin> collect = dtos.stream().map(StartedCabinDTO::toEtt).collect(Collectors.toList());
        Long lineId = collect.get(0).getLineId();
        Set<Long> except = collect.stream().map(StartedCabin::getId).collect(Collectors.toSet());
        this.removeByLineId(lineId, except);
        return this.saveOrUpdateBatch(collect);
    }

    private void removeByLineId(Long lineId, Set<Long> except) {
        this.lambdaUpdate()
                .set(StartedCabin::getIsDeleted, 1)
                .eq(StartedCabin::getLineId, lineId)
                .notIn(StartedCabin::getId, except)
                .update();
    }

    @Override
    public Map<String, StartedCabinVO> mapStartedCabinTon(BladeUser bladeUser, Long planId) {
        List<StartedCabin> list = this.lambdaQuery()
                .select(
                        StartedCabin::getCabin,
                        StartedCabin::getPlanTon,
                        StartedCabin::getIsReal
                )
                .eq(StartedCabin::getPlanId, planId)
                .eq(StartedCabin::getIsDeleted, 0)
                .list();

        Map<String, List<StartedCabin>> collect = list.stream().collect(Collectors.groupingBy(StartedCabin::getCabin));
        Map<String, StartedCabinVO> res = new HashMap<>();
        for (Map.Entry<String, List<StartedCabin>> entry : collect.entrySet()) {
            StartedCabinVO vo = new StartedCabinVO();
            String key = entry.getKey();
            List<StartedCabin> value = entry.getValue();
            Map<Integer, List<StartedCabin>> prMap = value.stream().collect(Collectors.groupingBy(StartedCabin::getIsReal));

            if (!prMap.isEmpty()) {
                if (prMap.containsKey(0)) {
                    vo.setPlanTon(getTon(prMap.get(0)));
                }

                if (prMap.containsKey(1)) {
                    vo.setWorkTon(getTon(prMap.get(1)));
                }
            }
            res.put(key, vo);
        }

        return res;
    }

    private BigDecimal getTon(List<StartedCabin> ls) {
        return ls.stream().map(StartedCabin::getPlanTon).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

}